﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using DLL.Entities.Event;
using DLL.Entities.Status;

namespace DLL.Entities
{
    public class WriteXML
    {
        RADStatus RS_Obj = new RADStatus();
        MISStatus MS_Obj = new MISStatus();
        RADEvent RE_Obj = new RADEvent();
        COMEvent CE_Obj = new COMEvent();
        MISEvent ME_Obj = new MISEvent();

        StreamWriter textWriter = null;

        #region Write Statuses
        public void WriteRADStatus(string dir)
        {
            RS_Obj.CurrentStatus = DateTime.Now;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(RADStatus));
                textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, RS_Obj);
                textWriter.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                textWriter.Close();
            }
        }

        public void WriteMISStatus(int missileCount, string dir)
        {
            MS_Obj.CurrentStatus = DateTime.Now;
            MS_Obj.MissileCount = missileCount;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MISStatus));
                textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, MS_Obj);
                textWriter.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                textWriter.Close();
            }
        }
        #endregion

        #region Write Events
        public void WriteRADEvent(TargetInfo ti, string dir)
        {
            RE_Obj.Altitude = ti.Altitude;
            RE_Obj.Speed = ti.Speed;
            RE_Obj.Distance = ti.Distance;
            RE_Obj.Position = ti.Position;
            RE_Obj.Heading = ti.Heading;
            RE_Obj.DetectionTime = DateTime.Now;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(RADEvent));
                textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, RE_Obj);
                textWriter.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                textWriter.Close();
            }
        }

        public void WriteCOMEvent(int count, int alt, int spd, int dist, int on, string dir)
        {
            CE_Obj.LessOne = count;
            CE_Obj.Altitude = alt;
            CE_Obj.Speed = spd;
            CE_Obj.Distance = dist;
            CE_Obj.On = on;
            
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(COMEvent));
                textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, CE_Obj);
                textWriter.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                textWriter.Close();
            }
        }

        public void WriteMISEvent(string dir)
        {
            ME_Obj.TimeFired = DateTime.Now;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MISEvent));
                textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, ME_Obj);
                textWriter.Close();
            }
            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                textWriter.Close();
            }
        }
        #endregion
    }
}
